package com.springboot.news.controller;

import com.springboot.news.domain.po.Article;
import com.springboot.news.domain.po.PageBean;
import com.springboot.news.domain.po.Result;
import com.springboot.news.service.ArticleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @GetMapping("/list")
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize, @RequestParam(required = false) Integer categoryId,@RequestParam(required = false) String status) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, status);
        return Result.success(pb);
    }
    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
         articleService.delete(id);
         return Result.success();
    }
    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id) {
        Article article = articleService.detail(id);
        return Result.success(article);
    }
    @PostMapping("update")
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

}


